package com.dailycodebuffer.springbootmongodb.service;


import com.dailycodebuffer.springbootmongodb.collection.Product;
import com.dailycodebuffer.springbootmongodb.repository.ProductRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;


/**
 * 业务层
 *
 * @author makejava
 * @since 2025-01-04 00:23:42
 */
@Service
public class ProductServiceImpl implements ProductService {

	@Resource
    private ProductRepository productRepository;

    @Override
    public void save(Product product) {
        productRepository.save(product);
    }

//    @Override
//    public void deleteById(String id) {
//        productRepository.delete(id);
//    }
//
@Override
public Product findById(Integer id) {
    return productRepository.findById(id)
            .orElse(null); // 或者抛出异常、返回默认值等
}
//
//    @Override
//    public List<Product> findById(Collection<String> ids) {
//        Iterable<Product> iterable = productRepository.findAll(ids);
//        return StreamSupport.stream(iterable.spliterator(), false)
//                .collect(Collectors.toList());
//    }

    @Override
    public Page<Product> list(Pageable page) {
        return productRepository.findAll(page);
    }

}

